<template>
  <div class="member-order">
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPannel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name"
      >
      </XtxTabsPannel>
    </XtxTabs>
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
        v-for="item in orderList"
        :key="item.id"
        :order="item"
        @on-cancel="handleCancel"
        @on-delete='deleteOrder'
        @on-confirmGoods='confirmGoods'
        @on-logistics='orderonLogistics'
      ></OrderItem>
    </div>
    <XtxPagination
      :total="total"
      :pageSize="reqParams.pageSize"
      :current-page="reqParams.page"
      v-if="total > 0"
      @current-change="reqParams.page = $event"
    ></XtxPagination>
  </div>
  <orderCancel ref="target"></orderCancel>
  <OrderLogistics ref="refLogistics"></OrderLogistics>
</template>

<script>
import { ref, reactive, watch } from "vue";
import { orderStatus } from "@/api/constants";
import OrderItem from "./components/order-item.vue";
import { myOrders,DeleteOrderApi,ConfirmGoodsApi } from "@/api/order";
import orderCancel from "./components/order-cancel.vue"
import Confrim from "@/components/library/Confrim"
import Message from "@/components/library/message"
import OrderLogistics from "./components/order-logistics.vue"
export default {
  name: "MemberOrder",
  components: {
    OrderItem,
    orderCancel,
    OrderLogistics
  },
  setup() {
    const activeName = ref("all");
    // const changeTab=(tab)=>{
    //   console.log(tab);
    // }
    //获取中数据
    const reqParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0,
    });
    const loading = ref(false);
    const orderList = ref([]);
    const total = ref(0);
    //封装重新获取数据
   const findOrderListFn=()=>{
      loading.value = true;
        myOrders(reqParams).then((data) => {
          console.log(data);
          orderList.value = data.result.items;
          total.value = data.result.counts;
          loading.value = false;
        });
   }
    //监听reqParams变化，重新获取数据
    watch(
      reqParams,
      () => {
        findOrderListFn()
      },
      { immediate: true }
    );
    //删除订单
  const deleteOrder=(order)=>{
    Confrim({text:'确定删除此订单',}).then(()=>{
      DeleteOrderApi(order).then(()=>{
        Message({type:'success',text:'删除成功'})
         findOrderListFn()
      })
    })
  }
  
    const tabClick = ({ index }) => {
      // console.log(index);
      reqParams.orderState = index;
    };
    return {
      activeName,
      orderStatus,
      orderList,
      tabClick,
      loading,
      total,
      reqParams,
      ...useCancel(),
      deleteOrder,
      ...useConfirm(),
      ...uselogistics()
    };
  }
  
};
//取消订单
export const useCancel=()=>{
  const target=ref(null)
    const handleCancel=(order)=>{
      target.value.open(order)
    }
    return {target,handleCancel}
  }
  //确定收货
 export const useConfirm=()=>{
   const confirmGoods=(order)=>{
      Confrim({text:'确定收货,货款会直接进入商家账户'}).then(()=>{
        ConfirmGoodsApi(order.id).then(()=>{
          Message({type:'success',text:'已成功确定收货'})
          order.orderState=4
        })
      })
    }
    return {confirmGoods}
  }
//查看物流
export const uselogistics=()=>{
 const refLogistics=ref(null)
  const orderonLogistics=(order)=>{
    refLogistics.value.open(order)
  }
  return {orderonLogistics,refLogistics}
}
</script>

<style scoped lang="less">
.member-order {
  height: 100%;
  background-color: #fff;
}
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
  min-height: 400px;
  .loading {
    height: 100%;
    width: 100%;
    position: absolute;
    left: 0;
    top: 0;
    background: rgba(255, 255, 255, 0.9) url(../../../assets/images/loading.gif)
      no-repeat center;
  }
  .none {
    height: 400px;
    text-align: center;
    line-height: 400px;
    color: #999;
  }
}
</style>